﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls.DataVisualization.Charting;
using dotnetCHARTING.WinForms;
using ViCode_LeVi.Controls.Charts;
using ViCode_LeVi.Controls.Reports;
using DevExpress.XtraReports.UI;
using System.Collections.Specialized;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabAnalysis_Tab3_Win.xaml
    /// </summary>
    public partial class VMTabAnalysis_Tab3_Win : UserControl
    {
        public VMTabAnalysis_Tab3_Win()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabAnalysis_Tab3_Win_Loaded);

            //barChart.Palette = new System.Collections.ObjectModel.Collection<ResourceDictionary>();
            //System.Windows.Controls.DataVisualization.ResourceDictionaryCollection
            //barChart.DataContext = dataContext;
            StackPanel_Config.DataContext = VMTabAnalysis_Tab3_Win_Config.Intance;
            
            VMTabAnalysis_Tab3_Win_Config.Intance.PropertyChanged += new PropertyChangedEventHandler(Intance_PropertyChanged);

            //XtraReport_PotentialPortfolioChart x = new XtraReport_PotentialPortfolioChart();
            //x.CreateDocument();
            //x.ShowPreview();
        }

        void Intance_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //RefreshChart();
        }
        ObservableCollection<VMTabAnalysis_Tab3_Win_Item> dataContext = new ObservableCollection<VMTabAnalysis_Tab3_Win_Item>();
        void VMTabAnalysis_Tab3_Win_Loaded(object sender, RoutedEventArgs e)
        {
            //RefreshChart();            
            PotentialPortfolioChart.Func_GetStep_TaskItemGroup = () => 
                {
                    var codes = List_CheckBox.Where(c => c.IsChecked == true).Select(c => c.DataContext as Step_TaskItemGroup).Where ( c=> c!= null).Select ( c=> c.Code);
                    return RootData.TaskItemGroups_Source.Where(c => codes.Contains(c.Code)).ToArray();
                };
            Option_RunFirst();
            RefreshChart();
        }

       
        #region Option
        private void Option_RunFirst()
        {
            StackPanel_TaskType_LoadData();
            ComboBox_Aligment_LoadData();
            ComboBox_ChartLabelMode_LoadData();
            CheckBox_XAxisAreaZoom_Setup();
            ComboBox_ChartLegendMode_Setup();
        }
        private void ComboBox_ChartLegendMode_Setup()
        {
            ComboBox_ChartLegendMode.ItemsSource = Enum.GetValues(typeof(eChartLegendMode));
            ComboBox_ChartLegendMode.SelectedIndex = 0;

            PotentialPortfolioChart.ChartLegendMode = (eChartLegendMode)ComboBox_ChartLegendMode.SelectedValue;
            ComboBox_ChartLegendMode.SelectionChanged -= new SelectionChangedEventHandler(ComboBox_ChartLegendMode_SelectionChanged);
            ComboBox_ChartLegendMode.SelectionChanged += new SelectionChangedEventHandler(ComboBox_ChartLegendMode_SelectionChanged);

            PotentialPortfolioChart.LegendWithPercent= CheckBox_LegendWithPercent.IsChecked == true;
            CheckBox_LegendWithPercent.Click -= new RoutedEventHandler(CheckBox_LegendWithPercent_Click);
            CheckBox_LegendWithPercent.Click += new RoutedEventHandler(CheckBox_LegendWithPercent_Click);
        }

        void CheckBox_LegendWithPercent_Click(object sender, RoutedEventArgs e)
        {
            PotentialPortfolioChart.LegendWithPercent = CheckBox_LegendWithPercent.IsChecked == true;
            RefreshChart();
        }

        void ComboBox_ChartLegendMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBox_ChartLegendMode.SelectedValue != null)
            {
                PotentialPortfolioChart.ChartLegendMode = (eChartLegendMode)ComboBox_ChartLegendMode.SelectedValue;
                RefreshChart();
            }
        }

        private void CheckBox_XAxisAreaZoom_Setup()
        {
            PotentialPortfolioChart.XAxisAreaZoom = CheckBox_XAxisAreaZoom.IsChecked == true;
            CheckBox_XAxisAreaZoom.Click -= new RoutedEventHandler(CheckBox_XAxisAreaZoom_Click);
            CheckBox_XAxisAreaZoom.Click += new RoutedEventHandler(CheckBox_XAxisAreaZoom_Click);
        }

        void CheckBox_XAxisAreaZoom_Click(object sender, RoutedEventArgs e)
        {
            PotentialPortfolioChart.XAxisAreaZoom = CheckBox_XAxisAreaZoom.IsChecked==true;
            RefreshChart();
        }

        private void ComboBox_ChartLabelMode_LoadData()
        {
            ComboBox_ChartLabelMode.ItemsSource = Enum.GetValues(typeof(eChartLabelMode));
            ComboBox_ChartLabelMode.SelectedIndex = 0;

            PotentialPortfolioChart.ChartLabelMode = (eChartLabelMode)ComboBox_ChartLabelMode.SelectedValue;
            ComboBox_ChartLabelMode.SelectionChanged -= new SelectionChangedEventHandler(ComboBox_ChartLabelMode_SelectionChanged);
            ComboBox_ChartLabelMode.SelectionChanged += new SelectionChangedEventHandler(ComboBox_ChartLabelMode_SelectionChanged);
            
        }

        void ComboBox_ChartLabelMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBox_ChartLabelMode.SelectedValue != null)
            {
                PotentialPortfolioChart.ChartLabelMode = (eChartLabelMode)ComboBox_ChartLabelMode.SelectedValue;
                RefreshChart();
            }
        }

        private void ComboBox_Aligment_LoadData()
        {
            ComboBox_Aligment.ItemsSource = Enum.GetValues(typeof(LabelAlignment));
            ComboBox_Aligment.SelectedIndex = 0;

            PotentialPortfolioChart.LabelAlignment = (LabelAlignment)ComboBox_Aligment.SelectedValue;
            ComboBox_Aligment.SelectionChanged -= new SelectionChangedEventHandler(ComboBox_Aligment_SelectionChanged);
            ComboBox_Aligment.SelectionChanged += new SelectionChangedEventHandler(ComboBox_Aligment_SelectionChanged);
        }

        void ComboBox_Aligment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBox_Aligment.SelectedValue != null)
            {
                PotentialPortfolioChart.LabelAlignment = (LabelAlignment)ComboBox_Aligment.SelectedValue;
                RefreshChart();
            }
        }
        List<CheckBox> List_CheckBox = new List<CheckBox>();
        private void StackPanel_TaskType_LoadData()
        {
            StackPanel_TaskType.Children.Clear();
            List_CheckBox.Clear();
            var data = RootData.Intance.TaskItemGroupOfStep4.ToArray();
            foreach (var d in data)
            {
                CheckBox chk = new CheckBox() { DataContext = d, Content = d.Code , IsChecked= true};
                StackPanel_TaskType.Children.Add(chk);
                List_CheckBox.Add(chk);
                chk.Click -= new RoutedEventHandler(chk_Click);
                chk.Click += new RoutedEventHandler(chk_Click);
            }
            RefreshChart();
        }

        void chk_Click(object sender, RoutedEventArgs e)
        {
            RefreshChart();
        }
        #endregion
        public void RefreshChart()
        {            
            PotentialPortfolioChart.CreateChart();                  
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //XtraReport_PotentialPortfolioChart rpt = new XtraReport_PotentialPortfolioChart();
            //rpt.Title = "Potentially Portfolio";
            //rpt.CreateDocument();
            //rpt.ShowPreview();

            ChartReport chart = new ChartReport();
            chart.Title = "Potentially Portfolio";
            chart.AddChart(PotentialPortfolioChart);
            chart.CreateReport(new StringCollection());
            XtraReport_PotentialPortfolioChart.ShowReportPreview(chart);
        }
    }

    public class VMTabAnalysis_Tab3_Win_Config : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public void SendPropertyChanged(string sProp)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(sProp));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public static VMTabAnalysis_Tab3_Win_Config Intance = new VMTabAnalysis_Tab3_Win_Config();

        #region Display_Y
        #region Display_Y_ID
        private bool _Display_Y_ID = true;
        public bool Display_Y_ID
        {
            get { return _Display_Y_ID; }
            set
            {
                if (_Display_Y_ID != value)
                {
                    _Display_Y_ID = value;
                    SendPropertyChanged("Display_Y_ID");
                }
            }
        }
        #endregion
        #region Display_Y_Name
        private bool _Display_Y_Name = false;
        public bool Display_Y_Name
        {
            get { return _Display_Y_Name; }
            set
            {
                if (_Display_Y_Name != value)
                {
                    _Display_Y_Name = value;
                    SendPropertyChanged("Display_Y_Name");
                }
            }
        }
        #endregion
        #endregion

        #region Display_X
        #region Display_X_Weight
        private bool _Display_X_Weight = true;
        public bool Display_X_Weight
        {
            get { return _Display_X_Weight; }
            set
            {
                if (_Display_X_Weight != value)
                {
                    if (value == true)
                    {
                        Display_X_DisableAll();
                        Display_X_Raise();
                    }
                    _Display_X_Weight = value;
                    SendPropertyChanged("Display_X_Weight");
                    Display_X_NeedOne();
                }
            }
        }
        #endregion
        #region Display_X_Quantity
        private bool _Display_X_Quantity = true;
        public bool Display_X_Quantity
        {
            get { return _Display_X_Quantity; }
            set
            {
                if (_Display_X_Quantity != value)
                {
                    if (value == true)
                    {
                        Display_X_DisableAll();
                        Display_X_Raise();
                    }
                    _Display_X_Quantity = value;
                    SendPropertyChanged("Display_X_Quantity");

                    Display_X_NeedOne();
                }
            }
        }
        #endregion
        #region Display_X_Quanlity
        private bool _Display_X_Quanlity = false;
        public bool Display_X_Quanlity
        {
            get { return _Display_X_Quanlity; }
            set
            {
                if (_Display_X_Quanlity != value)
                {
                    if (value == true)
                    {
                        Display_X_DisableAll();
                        Display_X_Raise();
                    }
                    _Display_X_Quanlity = value;
                    SendPropertyChanged("Display_X_Quanlity");
                    Display_X_NeedOne();
                }
            }
        }
        #endregion
        #region Display_X_Systematic
        private bool _Display_X_Systematic = false;
        public bool Display_X_Systematic
        {
            get { return _Display_X_Systematic; }
            set
            {
                if (_Display_X_Systematic != value)
                {
                    if (value == true)
                    {
                        Display_X_DisableAll();
                        Display_X_Raise();
                    }
                    _Display_X_Systematic = value;
                    SendPropertyChanged("Display_X_Systematic");
                    Display_X_NeedOne();
                }
            }
        }
        #endregion
        private void Display_X_DisableAll()
        {
            _Display_X_Weight = false;
            _Display_X_Quanlity = false;
            _Display_X_Quantity = false;
            _Display_X_Systematic = false;
        }
        private void Display_X_Raise()
        {
            SendPropertyChanged("Display_X_Weight");
            SendPropertyChanged("Display_X_Quanlity");
            SendPropertyChanged("Display_X_Quantity");
            SendPropertyChanged("Display_X_Systematic");
        }
        private void Display_X_NeedOne()
        {
            if (_Display_X_Weight == false &&
            _Display_X_Quanlity == false &&
            _Display_X_Quantity == false &&
            _Display_X_Systematic == false)
            {
                _Display_X_Weight = true;
                Display_X_Raise();
            }
        }
        #endregion
    }

    public class VMTabAnalysis_Tab3_Win_Item : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public void SendPropertyChanged(string sProp)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(sProp));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public VMTabAnalysis_Tab3_Win_Item(Step_TaskItem _Task)
        {
            Task = _Task;
            VMTabAnalysis_Tab3_Win_Config.Intance.PropertyChanged += new PropertyChangedEventHandler(Intance_PropertyChanged);
        }

        void Intance_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            SendPropertyChanged("X");
            SendPropertyChanged("Y");
            SendPropertyChanged("Size");
        }
        Step_TaskItem Task;
        public int X_Quanlity
        {
            get
            {
                return Task.Quanlity_WeightPercent.HasValue ? Task.Quanlity_WeightPercent.Value : 0;
            }
        }
        public int X_Quantity
        {
            get
            {
                return Task.Quantity_WeightPercent.HasValue ? Task.Quantity_WeightPercent.Value : 0;
            }
        }
        public int X_System
        {
            get
            {
                return Task.Systematic_WeightPercent.HasValue ? Task.Systematic_WeightPercent.Value : 0;
            }
        }
        public int X
        {
            get
            {
                return Task.Weight;//(X_Quanlity + X_Quantity + X_System) / 3.0;
            }
        }
        public int Y
        {
            get
            {
                return Task.Weight;
            }
        }
        public int Size
        {
            get { return 100; }
        }
    }

    public class VMTabAnalysis_Tab3_Win_Item_Color_Converter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Brushes.Yellow;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
